 var base_url = 'http://www.wanqi168.com/index.php/';
// var base_url = 'http://localhost/Shop/index.php/';
//  var base_url = 'http://123.57.83.8/Shop/index.php/';
 var app = angular.module('introduction', []);
app.directive('mouse-over-leave', function(){
	return {
		restrict: 'A',

		link: function(scope, elem, attr){
			$(".jqzoom").imagezoom();
			$("#thumblist li a").click(function() {
				$(this).parents("li").addClass("tb-selected").siblings().removeClass("tb-selected");
				$(".jqzoom").attr('src', $(this).find("img").attr("mid"));
				$(".jqzoom").attr('rel', $(this).find("img").attr("big"));
			});
		}
	}
});

app.controller('product',  function($scope,$http,$sce){
	$scope.page ={
		cpage:1,
		totalnum:1
	};
	// $scope.product = {
	// 	images:[{
	// 		small:'',
	// 		mid:'',
	// 		big:'',
	// 	},
	// 		{
	// 			small:'',
	// 			mid:'',
	// 			big:'',
	// 		},
	// 		{
	// 			small:'',
	// 			mid:'',
	// 			big:'',
	// 		}
	// 	],
	// 	title:'',
	// 	price:56.90,
	// 	vip_price:44.90,
	// 	pro_price:76.00,
	// 	sales:6015,
	// 	repertory:10000,
	// 	desc:[ '',
	// 	],
	// 	attribute:[
	// 		{name:'',value:''},
	// 	],
	// };
	$scope.product = {
		images:[
			{
			small:'Public/images/01_small.jpg',
			mid:'Public/images/01_mid.jpg',
			big:'Public/images/01.jpg',
		    },
			{
			small:'Public/images/01_small.jpg',
			mid:'Public/images/01_mid.jpg',
			big:'Public/images/01.jpg',
		    },
		],
		title:'良品铺子 手剥松子218g 坚果炒货 巴西松子',
		price:56.90,
		vip_price:44.90,
		pro_price:76.00,
		sales:6015,
		repertory:10000,
		desc:[ 'Public/images/tw1.jpg',
		],
		attribute:[
			{name:'产品类型',value:'烘炒类'},
		]
	};
	var goods_id = $('#goods_id_hidden').val();
	var data = {
			goods_id: goods_id
		};
	//post请求的地址
	url = base_url+"Home/Goods/goodsDetails";
//将参数传递的方式改成form
	var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};
//发送post请求，获取数据
	$http.post(url, data,postCfg)
		.success(function(data) {
			console.info(data.goods);
			$scope.product = data.goods;
			// $scope.product.content = $sce.trustAsHtml(data.goods.content);
			$scope.evaluaters = data.comments.comments;
			$scope.page = data.comments.page;
			$scope.evaluate = data.comments_data;
			$scope.pagenumCount();
			// $("#content").empty().html(data.goods.content);
			$scope.set_img_width();
		});
	//图文详情图片宽度设置
	$scope.set_img_width = function() {
		$("#content p").find("img").width("100%");
		var height = 0;
		$("#content").find("p").each(function(index){

			height = height + $(this).children().height();
			console.log($(this).children().height());
		});

		$("#content").height(height +"px");
	};

/*	$scope.products = [
		{
			title:'【三只松鼠_开口松子】零食坚果特产炒货东北红松子原味',
			imageurl:'images/browse1.jpg',
			price:35.90
		},{
			title:'【三只松鼠_开口松子】零食坚果特产炒货东北红松子原味',
			imageurl:'images/browse1.jpg',
			price:35.90
		},{
			title:'【三只松鼠_开口松子】零食坚果特产炒货东北红松子原味',
			imageurl:'images/browse1.jpg',
			price:35.90
		},{
			title:'【三只松鼠_开口松子】零食坚果特产炒货东北红松子原味',
			imageurl:'images/browse1.jpg',
			price:35.90
		},{
			title:'【三只松鼠_开口松子】零食坚果特产炒货东北红松子原味',
			imageurl:'images/browse1.jpg',
			price:35.90
		},
	];*/

	//用户名匿名化
	$scope.formatName = function() {
		angular.forEach($scope.evaluaters, function(value, key){
			if(value.name.length <=1) {
				value.name = value.name.substr(0, 1)+"****";
			}else
			value.name = value.name.substr(0, 1)+"****"+value.name.substr(-1);
		});
		$scope.pagenumCount();
	}
	//当前页数和总页数
	//分页的显示几个页数的计算，默认只显示5页
	$scope.pagenumCount = function(){
		var num = 5;
		var mid = num/2;
		if( num % 2 != 0) {
			mid = (num-1)/2;
		}
		var i = [];
		var startpage,endpage;
		if($scope.page.totalnum<=5){
			startpage = 1;
			endpage = $scope.page.totalnum;
		}else{
			startpage = $scope.page.cpage-3;
			endpage = $scope.page.cpage+2;
			if(startpage<1){
				startpage = 1;
				endpage = 5;
			}
			if(endpage>$scope.page.totalnum){
				endpage = $scope.page.totalnum;
				startpage = $scope.page.totalnum-4;
			}
		}
		for(var x = 0; x < (endpage - startpage +1); x++) {
			i[x] = startpage + x;
		}
		$scope.page.pagenum  = i;
	};
	//分页点击完的数据更新
	//分页点击事件
	$scope.pageclick = function(page){
		if(page == "«") {
			if($scope.page.cpage > 1) {
				$scope.page.cpage -= 1;
			}
		} else if(page == "»") {
			if($scope.page.cpage < $scope.page.totalnum)
				$scope.page.cpage += 1;
		} else {
			$scope.page.cpage=page;
		}
		var data={goods_id: goods_id,pagenum:$scope.page.cpage};
		var url = base_url+"Home/Goods/commentListByPage",
			postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};
		$http.post(url, data,postCfg)
			.success(function(data) {
				$scope.evaluaters = data.comments;
				// $scope.page = data.page;
				$scope.pagenumCount();
			});
	};
	

})
